package user

import (
	"context"
	"fmt"
	"github.com/jinzhu/copier"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"large-model-platform-backend/internal/svc"
	"large-model-platform-backend/internal/types"
	model "large-model-platform-backend/mongo"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveLogic {
	return &SaveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SaveLogic) Save(req *types.User) (resp *types.BooleanResp, err error) {
	model1 := l.svcCtx.UserModel

	var chats []model.ChatSession
	var messages []model.Message
	messages = append(messages, model.Message{
		Content: "",
		Role:    "",
	})
	// 填充默认的数据结构，否则后续数据结构不好处理
	//chats = append(chats, model.ChatSession{
	//	Name:     "default",
	//	Messages: messages,
	//})

	common := model.UserCommon{}
	copier.Copy(&common, req)
	user := model.User{
		ID:           primitive.NilObjectID,
		UserCommon:   common,
		ChatSessions: chats,
	}
	fmt.Println(user)
	err = model1.Insert(l.ctx, &user)
	return &types.BooleanResp{
		Result: types.Result{Code: 200},
		Data:   true,
	}, nil
}
